package com.hundsun.yr.universal.p2p.utils;

import android.content.Context;

import com.hundsun.yr.universal.library.common.MD5Utils;
import com.hundsun.yr.universal.library.common.PackageUtils;
import com.hundsun.yr.universal.library.common.TelephoneUtils;
import com.hundsun.yr.universal.library.widget.HSToast;
import com.umeng.socialize.utils.Log;

import java.util.Date;
import java.util.HashMap;
import java.util.TimeZone;

/**
 * Created by xx on 2017/4/16.
 * 网络请求的工具类
 */
public class BaseRequestTool2 {

    /**
     * 可能会造成内存泄漏，请求多的时候不知道会不会出问题
     */
//    public static String linuxTime;
    /**
     * 获得请求头，填写的参数是为了生成通过验签
     * 问题1、那些头是必须的
     *
     * @param mContext
     * @param method
     * @param url
     * @param key
     * @return
     */
    public static HashMap<String, String> getHearder(Context mContext, String method, String url, String user_no, String key, String zwgt_data) {
        try{
            String szwgt_data = zwgt_data.toLowerCase();
            HashMap<String, String> header = new HashMap<String, String>();
            //检查权限
            String imei = TelephoneUtils.getIMEI(mContext);
            if (imei == null || imei.isEmpty()){
                imei = "";
            }

            header.put("X-Device-Id", imei.toLowerCase());
            header.put("X-Platform", "Android"  + android.os.Build.VERSION.RELEASE);
            header.put("X-Version", PackageUtils.getVersionName(mContext));
            header.put("X-ZWGT-DATE", zwgt_data);
            String user_no_low = user_no.toLowerCase();
            header.put("x-auth", signSHA1(mContext, method, url, user_no_low, szwgt_data, key));
            header.put("x-user-no", user_no);
//            Logs.e("version: " + PackageUtils.getVersionName(mContext));
            Log.e("HEader",header.toString());
            return header;
        } catch (SecurityException e){
//            Logs.e("xushunzhen: " + e.toString());
            new HSToast(mContext).showToast("如果您未开启相对应权限我们将无法为您服务");
            return new HashMap<>();
        }
    }

    /**
     * 此函数非原创，从网上搜索而来，timeZoneOffset原为int类型，为班加罗尔调整成float类型
     * timeZoneOffset表示时区，如中国一般使用东八区，因此timeZoneOffset就是0
     *
     * @return
     */
    public static String getFormatedDateString() {
        TimeZone.setDefault(TimeZone.getTimeZone("GMT+0"));
        Date date = new Date();

        String[] k = (date + "").split(" ");
        Log.e("k" + k[0] + ", " + k[2] + " " + k[1] + " " + k[5] + " " + k[3] + " GMT");
//    	System.out.println(k[0]+","+k[2]+" "+k[1]+" "+k[5] +" "+k[3]+" +0000");
        return k[0] + ", " + k[2] + " " + k[1] + " " + k[5] + " " + k[3] + " GMT";
    }

    /**
     * 生成验签
     * todo 加密异常的时候会报错，导致auth空异常
     * @param mContext
     * @param methed
     * @param url
     * @param key
     * @return
     */
    public static String signSHA1(Context mContext, String methed, String url, String user_no ,String zwgt_data, String key) {
        try {
            //sha1签名字符串拼接
            String imei = TelephoneUtils.getIMEI(mContext);
            if (imei == null){
                imei = "";
            }

            String signature = methed + "\n" +
                    url + "\n" +
                    user_no + "\n" +//user_no可以为空
                    imei.toLowerCase() + "\n" +
                    zwgt_data + "\n";
            return HMACSHA1.HmacSHA1Encrypt(signature, key);
        } catch (Exception e){
            return "";
        }
    }

    /**
     * post请求的时候生成验签
     *
     * @param timeStamp
     * @param content
     * @return
     */
    public static String getSign(String timeStamp, String content) {
        Logs.e("timeStamp: " + timeStamp);
        String sign = timeStamp + "\n"
                + content;
        return MD5Utils.getMD5String(sign);
    }

    /**
     * 加密成data
     *
     * @param timeStamp
     * @param key
     * @param content
     * @return
     */
    public static String getData(String timeStamp, String key, String content, String zwgt_data_time) {
        String sign = timeStamp + "\n"
                + content;
        try {
            return AndroidDes3Util.encode(sign, key, zwgt_data_time);
        } catch (Exception e) {
            return "";
        }
    }
}
